package com.bll.tsdoa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bll.tsdoa.dto.PatternDto;
import com.bll.tsdoa.entity.Cutting;
import com.bll.tsdoa.entity.Part;
import com.bll.tsdoa.entity.Pattern;
import com.bll.tsdoa.entity.Raw;
import com.bll.tsdoa.mapper.CuttingDao;
import com.bll.tsdoa.mapper.PartDao;
import com.bll.tsdoa.mapper.PatternDao;
import com.bll.tsdoa.mapper.RawDao;
import com.bll.tsdoa.service.PatternService;
import com.bll.tsdoa.utils.CommonUtils;
import com.bll.tsdoa.utils.DefaultIdentifierGenerator;
import com.bll.tsdoa.utils.GaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 排样方案(Pattern)表服务实现类
 *
 * @author makejava
 * @since 2025-02-26 20:03:05
 */
@Service("patternService")
@Slf4j
public class PatternServiceImpl extends ServiceImpl<PatternDao, Pattern> implements PatternService {
    @Resource
    private DefaultIdentifierGenerator defaultIdentifierGenerator;
    @Resource
    private PatternDao patternDao;
    @Resource
    private GaUtils gaUtils;
    @Resource
    private CuttingDao cuttingDao;
    @Autowired
    private PartDao partDao;
    @Resource
    private RawDao rawDao;
    @Resource
    private CommonUtils commonUtils;

    @Override
    public List<PatternDto> getPattern() {
        int size = partDao.selectList(null).size();
        int count = 0;
        while (!this.isEmpty()) {
            List<int[]> initPolulation = gaUtils.initPolulation();
            List<int[]> selectCopy = gaUtils.selectCopy(initPolulation);
            List<int[]> cross = gaUtils.cross(selectCopy, 1);
            int[] result = gaUtils.simulatedAnnealing(cross);
            gaUtils.finish(result);

            if (count < size) {
                count++;
                log.info("当前进度{}", Double.toString(1.0 * count / size));
            }
        }

        List<PatternDto> patternDtoList = new ArrayList<>();
        List<Pattern> patternList = patternDao.selectList(null);
        for (Pattern pattern : patternList) {
            PatternDto patternDto = new PatternDto();
            BeanUtils.copyProperties(pattern, patternDto);
            List<Cutting> cuttingList = cuttingDao.selectList(new LambdaQueryWrapper<Cutting>()
                    .select(Cutting::getPartId, Cutting::getPartLength, Cutting::getPartNum)
                    .eq(Cutting::getCuttingId, pattern.getCuttingId()));
            patternDto.setCuttingList(cuttingList);
            patternDtoList.add(patternDto);
        }
        refresh();
        log.info("---------------------------Done---------------------------");
        return patternDtoList;

    }

    public boolean isEmpty() {
        List<Part> partList = partDao.selectList(null);
        for (Part part : partList) {
            if (part.getNum() != 0)
                return false;
        }
        return true;
    }

    @Override
    public boolean isError() {
        List<Part> partList = partDao.selectList(null);
        List<Raw> rawList = rawDao.selectList(null);
        Double maxRawLength = commonUtils.getMaxRawLength(rawList);
        Double maxPartLength = commonUtils.getMaxPartLength(partList);
        if (maxRawLength < maxPartLength)
            return true;
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void refresh() {
        patternDao.delete(null);
        cuttingDao.delete(null);
        rawDao.delete(null);
        partDao.delete(null);
    }
}

